﻿using System;
using System.Collections.Generic;

namespace FileZillaConfig.Types
{
    public class PortsList
    {
        private List<int> _ports;

        /// <summary>
        /// Create empty ports list
        /// </summary>
        public PortsList()
        {
            this._ports = new List<int>();
        }

        /// <summary>
        /// Add port to the list. Method prevents ports duplication
        /// </summary>
        /// <param name="port">Integer between 1 and 65535</param>
        public void addPort(int port)
        {
            if (port >= 1 && port <= 65535)
            {
                //Prevent port duplication
                if (this._ports.IndexOf(port) == -1)
                {
                    this._ports.Add(port);
                }
            }
            else
            {
                throw new System.Exception("Port value must be integer between 1 and 65535.");
            }
        }

        /// <summary>
        /// Remove specific port from the list 
        /// </summary>
        /// <param name="port">Integer between 1 and 65535</param>
        /// <returns>True if port has been removed otherwise false</returns>
        public bool removePort(int port)
        {
            return this._ports.Remove(port);
        }

        /// <summary>
        /// Parse delimited string and create new port list or gets port list as delimited string
        /// </summary>
        public string portString
        {
            set
            {
                this._ports = new List<int>();
                string[] p = value.Split(' ');
                foreach (string port in p)
                {
                    addPort(Convert.ToInt32(port));
                }
            } 
            get
            {
                return String.Join(" ", this._ports.ToArray());
            }
        }

        /// <summary>
        /// List of ports
        /// </summary>
        public List<int> List
        {
            get
            {
                return this._ports;
            }
        }



    }
}
